﻿#include "AppDelegate.h"
#include "HelloWorldScene.h"

USING_NS_CC;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate() 
{
}

//if you want a different context,just modify the value of glContextAttrs
//it will takes effect on all platforms
void AppDelegate::initGLContextAttrs()
{
    //set OpenGL context attributions,now can only set six attributions:
    //red,green,blue,alpha,depth,stencil
    GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};

    GLView::setGLContextAttrs(glContextAttrs);
}

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();

    auto glview = director->getOpenGLView();
    if(!glview) {
		struct tm *tm;
		time_t timep;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
		time(&timep);
#else  
		struct timeval tv;
		gettimeofday(&tv, NULL);
		timep = tv.tv_sec;
#endif  

		tm = localtime(&timep);
		int year = tm->tm_year + 1900;
		int month = tm->tm_mon + 1;
		int day = tm->tm_mday;
		int hour = tm->tm_hour;
		int minute = tm->tm_min;
		int second = tm->tm_sec;

		char sysTime[50] = { 0 };
		sprintf(sysTime, "%s[%d-%02d-%02d %02d:%02d:%02d]", "My Game",year, month, day, hour, minute, second);

		glview = GLViewImpl::create(sysTime);
		director->setOpenGLView(glview);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
		int screen_w = 960;
		int screen_h = 640;
		auto fullPath = FileUtils::getInstance()->fullPathForFilename("config.plist");
		if (!fullPath.empty())
		{
			auto *dict = Dictionary::createWithContentsOfFileThreadSafe(fullPath.c_str());
			if (dict)
			{
				auto *ScreenType = (Dictionary*)dict->objectForKey("ScreenType");
				// Dictionary *framesDict = (Dictionary*)dict->objectForKey("frames");
				if (ScreenType != NULL)
				{
					auto ScreenName = (CCString)ScreenType->valueForKey("ScreenName")->getCString();
					float scale = ScreenType->valueForKey("ScaleFactor")->floatValue();
					if (scale <= 0)
					{
						scale = 1;
					}
					auto *device = (CCDictionary*)dict->objectForKey(ScreenName.getCString());//取得当前设备
					screen_w = device->valueForKey("ScreenW")->intValue();
					screen_h = device->valueForKey("ScreenH")->intValue();
				}
			}
		}
		glview->setFrameSize(screen_w, screen_h);
#endif
    }


	{
		auto winSize = director->getWinSize();
		float screenWidth = winSize.width;
		float screenHeight = winSize.height;
		//缩放参照屏幕
		float visibleSizeWidth = 960;
		float visibleSizeHeight = 640;

		// 根据屏幕尺寸与设计尺寸的比，重新定义设计尺寸（参考kResolutionNum来适配）
		if (screenWidth / screenHeight > visibleSizeWidth / visibleSizeHeight)
		{
			visibleSizeWidth = visibleSizeHeight * screenWidth / screenHeight;
		}
		else if (screenWidth / screenHeight < visibleSizeWidth / visibleSizeHeight)
		{
			visibleSizeHeight = visibleSizeWidth / (screenWidth / screenHeight);
		}
		glview->setDesignResolutionSize(visibleSizeWidth, visibleSizeHeight, kResolutionNoBorder);
		std::vector<std::string> searchPath;
		searchPath.push_back("Resources");
		FileUtils::getInstance()->setSearchPaths(searchPath);
	}

    // turn on display FPS
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
    auto scene = HelloWorld::createScene();

    // run
    director->runWithScene(scene);

    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    Director::getInstance()->stopAnimation();

    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
